/*******************************************************************************
 * Copyright (c) 2011 by ProSyst Software GmbH
 * http://www.prosyst.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/
package activator;

import java.util.Dictionary;
import java.util.Hashtable;

import library.service.LibraryService;
import managed.service.factory.LibraryManagedServiceFactory;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ManagedServiceFactory;

import service.impl.VariedLibraryServiceImpl;

public class ManagedServiceFactoryActivator implements BundleActivator {

	private static final String MSF_FPID = "library.service.fpid";
	private ServiceRegistration managedServiceFactoryReg;
	private LibraryManagedServiceFactory libManagedServiceFactory;

	// --- BundleActivator interface implementation ----------------------------
	public void start(BundleContext context) throws Exception {
		System.out.println("[ Activator ]: start() method");

		// 1. ManagedServiceFactory creation
		libManagedServiceFactory = new LibraryManagedServiceFactory(context);

		// 2. managedServiceFactory Properties creation
		Dictionary managedServiceFactoryProps = new Hashtable();
		managedServiceFactoryProps.put(Constants.SERVICE_PID, MSF_FPID);

		// 3. ManagedServiceFactoryRegistration creation
		managedServiceFactoryReg = context.registerService(
				ManagedServiceFactory.class.getName(),
				libManagedServiceFactory, managedServiceFactoryProps);
	}

	public void stop(BundleContext context) throws Exception {
		// 1. cleanUp and unregister LibServiceReg, LibService
		libManagedServiceFactory.cleanUpRegisteredServices();
		libManagedServiceFactory = null;

		// 2. ManagedServiceFactory clean up
		if (managedServiceFactoryReg != null) {
			managedServiceFactoryReg.unregister();
			managedServiceFactoryReg = null;
		}

		System.out.println("[ Activator ]: stop() method");
	}
}
